package com.example.hello_world.train_Service;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.hello_world.R;

public class service2Acitivity extends AppCompatActivity {

    private Myservice02.MyBinder binder;
    private Button onbindBtn,unbindBtn,showcount;
    private ServiceConnection connection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            binder= (Myservice02.MyBinder) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service2_acitivity);

        onbindBtn=findViewById(R.id.onbind);
        unbindBtn=findViewById(R.id.unbind);
        showcount=findViewById(R.id.showcount);
        final Intent intent=new Intent(service2Acitivity.this,Myservice02.class);
        onbindBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bindService(intent,connection,BIND_AUTO_CREATE);
            }
        });

        unbindBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                unbindService(connection);
            }
        });

        showcount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Log.i("你好","你哈"+binder.getCount());
                Toast.makeText(getApplicationContext(),"count值：" +binder.getCount(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(connection);
    }
}